package com.mofang.powerdekorhelper.activity.mine;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.base.MvpActivity;
import com.mofang.powerdekorhelper.persenter.BindPhoneVerifyPerenster;
import com.mofang.powerdekorhelper.utils.Countdown;
import com.mofang.powerdekorhelper.utils.L;
import com.mofang.powerdekorhelper.utils.SharePerforenceUtils;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.view.BindPhoneVerifyView;
import com.mofang.powerdekorhelper.witget.TitleBar;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by luofei on 2016/7/29 0029.
 */

public class BindPhoneVerifyActivity extends MvpActivity<BindPhoneVerifyView, BindPhoneVerifyPerenster> implements BindPhoneVerifyView {

    public final static int toChangePhoneCode = 111;

    @BindView(R.id.change_binding_phone)
    TitleBar mTitleBar;

    @BindView(R.id.change_phone_already_bindphone)
    TextView alreadyBindphone;

    @BindView(R.id.change_phone_code_edit)
    EditText codeEdit;

    @BindView(R.id.change_phone_get_code)
    TextView validateTv;

    private Countdown timer;

    @Override
    public void initView() {

        inflateLayout(R.layout.activity_bind_phone_change_layout);
        initTitleBarWithback(mTitleBar, R.string.change_binding_phone);
    }

    @Override
    public void initData() {

        String bindphone = SharePerforenceUtils.getInstance(this).getPhone();
        if (!bindphone.equals("")) {
            String targetStr = bindphone.substring(3, 7);
            bindphone = bindphone.replace(targetStr, "****");
            alreadyBindphone.setText(bindphone);

        } else {
            alreadyBindphone.setText("暂未绑定手机号");
        }
    }

    @OnClick({R.id.change_phone_get_code, R.id.change_phone_sure})
    void OnClick(View view) {

        String code = codeEdit.getText().toString();
        String phone = SharePerforenceUtils.getInstance(this).getPhone();

        switch (view.getId()) {

            case R.id.change_phone_get_code:

                if (!TextUtils.isEmpty(phone)) {
                    presenter.getMesCode(phone);
                    verifyBtnState(false, "验证码已发送");
                }

                break;

            case R.id.change_phone_sure:

                if (!TextUtils.isEmpty(code)) {

                    int id = SharePerforenceUtils.getInstance(this).getUserID();

                    presenter.getBindPhoneVerify(id, code, phone, 0);
                } else {
                    T.showShort(this, "请输入验证码");
                }
                break;
        }
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void setVerifyResult(String info) {
        if (!info.equals("")) {
            if (info.contains("成功")) {
                startActivityForResult(new Intent(this, ChangePhoneSecondAcitvity.class), toChangePhoneCode);
            } else {
                T.showShort(this, "验证失败");
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == toChangePhoneCode) {
            finish();
        }
    }

    @Override
    public void setVCodeResult(String msg) {
        if (msg != null & !msg.equals("")) {
            if (msg.contains("成功")) {
                T.showShort(this, "验证码已发送,请稍候");
                timer = new Countdown();
                timer.createTimer(new Countdown.UpDataTime() {
                    @Override
                    public void updataTime(int time) {
                        L.i("SetMesCode", "timer----------------->" + time);
                        verifyBtnState(false, time + "秒后重发");
                        if (time > 0) {
                            validateTv.setEnabled(false);
                        } else {

                            verifyBtnState(true, "重新发送");
                        }
                    }
                });

            }
        } else {
            T.showShort(this, "验证码发送失败，请重试");
            verifyBtnState(true, "");
        }
    }

    @Override
    public BindPhoneVerifyPerenster initPresenter() {
        return new BindPhoneVerifyPerenster();
    }

    // 设置获取验证码的背景图片和文字
    private void verifyBtnState(Boolean state, String verifyStr) {
        validateTv.setText(verifyStr);
        validateTv.setEnabled(state);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.stop();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (timer != null) {
            timer.stop();
        }
    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void toast(CharSequence s) {

    }

    @Override
    public void toast(int id) {

    }

    @Override
    public void showNullLayout() {

    }

    @Override
    public void hideNullLayout() {

    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {

    }

    @Override
    public void hideErrorLayout() {

    }

    @Override
    public void onError(String errorInfo) {

    }

}
